package com.chaosyeb.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chaosyeb.server.pojo.Menu;
import com.chaosyeb.server.pojo.MenuRole;
import com.chaosyeb.server.pojo.RespBean;
import com.chaosyeb.server.pojo.Role;
import com.chaosyeb.server.service.IMenuRoleService;
import com.chaosyeb.server.service.IMenuService;
import com.chaosyeb.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description : 权限组角色功能
 * @Author : Chaos
 * @Date : 2021/12/12 16:50
 * @Version : 1.0
 **/
@RestController
@RequestMapping("/system/basic/premiss")
public class PremissController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;

    @ApiOperation(value = "添加角色")
    @PostMapping("/add")
    public RespBean add(@RequestBody Role role) {
        //判断是否有前缀ROLE_，如果没有前缀，则添加。security规定
        if (!role.getName().startsWith("ROLE_")) {
            role.setName("ROLE_" + role.getName());
        }
        //循环判断，如果name已存在，返回角色已存在
        List<Role> listRole = roleService.list();
        for (int i = 0; i < listRole.size(); i++) {
            Role role1 = listRole.get(i);
            if (role.getName().equals(role1.getName())) {
                return RespBean.error("角色已存在");
            }
        }
        if (roleService.save(role)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/delete/{rid}")
    public RespBean delete(@PathVariable Integer rid) {
        if (roleService.removeById(rid)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "更新角色")
    @PutMapping("/update")
    public RespBean update(@RequestBody Role role) {
        if (StringUtils.isEmpty(role.getId())) {
            return RespBean.error("角色不存在！");
        }
        Integer id = role.getId();
        //根据id跟新不一定必需是要传入id的值
        if (roleService.updateById(role)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "查找角色")
    @GetMapping("/find")
    public RespBean find() {
        //要什么参数，全给他查出来
        List<Role> listRole = roleService.list();
        return RespBean.success("查找成功", listRole);
    }

    @ApiOperation("查询所有菜单")
    @GetMapping("/menus")
    public List<Menu> getAllMenus() {
        return menuService.getAllMenus();
    }

    @ApiOperation("根据角色id查询所有菜单id")
    @GetMapping("/mid/{rid}")
    public List<Integer> getMidByRid(@PathVariable Integer rid) {
        //需要返回的是Integer类型，但查到的是自定义的MenuRole类型，采用stream流转成获取到菜单id
        return menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid", rid))
                .stream().map(MenuRole::getMid).collect(Collectors.toList());
    }

    @ApiOperation("更新角色菜单")
    @PutMapping("/updateMenuRole")
    public RespBean updateMenuRole(@RequestParam("rid") Integer rid, @RequestParam("mids") Integer[] mids) {
        return menuRoleService.updateMenuRole(rid, mids);
    }
}
